﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewstorePlateformeDAO;

namespace NewstorePlateformeApp.Controllers
{ 
    public class TB_StockController : Controller
    {
        private Newstore_PL_GOLD_BDDEntities db = new Newstore_PL_GOLD_BDDEntities();

        //
        // GET: /TB_Stock/

        public ViewResult Index()
        {
            var marchandise = db.Marchandise.Include("Lieux").Include("MARCHANDISE_CATALOGUE");
            return View(marchandise.ToList());
        }

        //
        // GET: /TB_Stock/Details/5

        public ViewResult Details(int id)
        {
            Marchandise marchandise = db.Marchandise.Single(m => m.idMarchandise == id);
            return View(marchandise);
        }

        //
        // GET: /TB_Stock/Create

        public ActionResult Create()
        {
            ViewBag.idLieux = new SelectList(db.Lieux, "idLieux", "codeLieu");
            ViewBag.idMARCHANDISE_CATALOGUE = new SelectList(db.MARCHANDISE_CATALOGUE, "idMarchandise_Catalogue", "libelle");
            return View();
        } 

        //
        // POST: /TB_Stock/Create

        [HttpPost]
        public ActionResult Create(Marchandise marchandise)
        {
            if (ModelState.IsValid)
            {
                db.Marchandise.AddObject(marchandise);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.idLieux = new SelectList(db.Lieux, "idLieux", "codeLieu", marchandise.idLieux);
            ViewBag.idMARCHANDISE_CATALOGUE = new SelectList(db.MARCHANDISE_CATALOGUE, "idMarchandise_Catalogue", "libelle", marchandise.idMARCHANDISE_CATALOGUE);
            return View(marchandise);
        }
        
        //
        // GET: /TB_Stock/Edit/5
 
        public ActionResult Edit(int id)
        {
            Marchandise marchandise = db.Marchandise.Single(m => m.idMarchandise == id);
            ViewBag.idLieux = new SelectList(db.Lieux, "idLieux", "codeLieu", marchandise.idLieux);
            ViewBag.idMARCHANDISE_CATALOGUE = new SelectList(db.MARCHANDISE_CATALOGUE, "idMarchandise_Catalogue", "libelle", marchandise.idMARCHANDISE_CATALOGUE);
            return View(marchandise);
        }

        //
        // POST: /TB_Stock/Edit/5

        [HttpPost]
        public ActionResult Edit(Marchandise marchandise)
        {
            if (ModelState.IsValid)
            {
                db.Marchandise.Attach(marchandise);
                db.ObjectStateManager.ChangeObjectState(marchandise, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.idLieux = new SelectList(db.Lieux, "idLieux", "codeLieu", marchandise.idLieux);
            ViewBag.idMARCHANDISE_CATALOGUE = new SelectList(db.MARCHANDISE_CATALOGUE, "idMarchandise_Catalogue", "libelle", marchandise.idMARCHANDISE_CATALOGUE);
            return View(marchandise);
        }

        //
        // GET: /TB_Stock/Delete/5
 
        public ActionResult Delete(int id)
        {
            Marchandise marchandise = db.Marchandise.Single(m => m.idMarchandise == id);
            return View(marchandise);
        }

        //
        // POST: /TB_Stock/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Marchandise marchandise = db.Marchandise.Single(m => m.idMarchandise == id);
            db.Marchandise.DeleteObject(marchandise);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}